package com.example.animation_01;

import android.animation.AnimatorInflater;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class AnimatorActivity extends AppCompatActivity implements View.OnClickListener {
    private Button mButton;
    private ImageView mImage;
    private TextView mText;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animator);
        mButton = findViewById(R.id.button);
        mImage = findViewById(R.id.image);
        mText = findViewById(R.id.text);
        mButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button) {
            //initAnimator();
            //initAnimatorJava();
            //initAnimatorJava2();
//            initAnimatorJava3();
//            intiObjectAnimator();
//            initObjectAnimator2();
            initObjectAnimator3();
        }
    }

    private void initAnimator() {
        ValueAnimator valueAnimator = (ValueAnimator) AnimatorInflater.loadAnimator(this, R.animator.animator);
        valueAnimator.setTarget(mImage);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                Log.e("test123", "---------------------------" + value);
                //mImage.setRotation(value * 360);
                //mImage.setRotationX(value * 360);
                //mImage.setRotationY(value * 360);
                //mImage.setAlpha(1 - value);
                //mImage.getLayoutParams().width = (int) (mImage.getLayoutParams().width * value);
                //mImage.requestLayout();
                mImage.setTranslationX(value * 100);
                mImage.setTranslationY(value * 300);
                mText.setText(value + "");
            }
        });
        valueAnimator.start();
    }

    private void initAnimatorJava() {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(1, 0, 1, 0, 1);
        valueAnimator.setDuration(4000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                Log.e("test123", "---------------------------" + value);
                //mImage.setScaleX(value);
                //mImage.setScaleY(value);
                mImage.setAlpha(value);
            }
        });
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.start();
    }

    private void initAnimatorJava2() {
        ValueAnimator valueAnimator = ValueAnimator.ofArgb(
                0xffff0000, 0xff00ff00, 0xff0000ff, 0xffffffff, 0xff000000);
        valueAnimator.setDuration(6000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                mText.setBackgroundColor(value);
            }
        });
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.start();
    }

    private void initAnimatorJava3() {
        Man boy = new Man("小王", 18);
        Man old = new Man("老王", 88);
        ValueAnimator valueAnimator = ValueAnimator.ofObject(new TypeEvaluator() {
            @Override
            public Object evaluate(float fraction, Object startValue, Object endValue) {
                Man boy = (Man) startValue;
                Man old = (Man) endValue;
                int age = (int) (boy.getAge() + (old.getAge() - boy.getAge()) * fraction);
                String name;
                if (age < 30) {
                    name = "小王";
                } else if (age < 60) {
                    name = "王哥";
                } else {
                    name = "老王";
                }
                Man man = new Man(name, age);
                return man;
            }
        }, boy, old);

        valueAnimator.setDuration(6000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                Man man = (Man) animation.getAnimatedValue();
                mText.setText(man.toString());
            }
        });
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.start();
    }
    private void intiObjectAnimator(){
//        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(
//                mImage, "alpha",1,0,1,0);
//            ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(
//                    mImage, "Rotation",360,180,270,90,360);
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(
                mImage, "translationY",0,300,-270,190,360,0);
        objectAnimator.setDuration(2500);
            objectAnimator.start();
        }
    private void initObjectAnimator2(){
        Man man=new Man("jim",0);
        ObjectAnimator objectAnimator = ObjectAnimator.ofInt(man, "age", 0, 100);
        objectAnimator.setInterpolator(new LinearInterpolator());
        objectAnimator.setDuration(4000);
        objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                mText.setText(man.toString());
                Log.d("", "onAnimationUpdate: ");
            }
        });
        objectAnimator.start();
    }
    private void initObjectAnimator3(){
        PropertyValuesHolder rotationHolder =PropertyValuesHolder.ofFloat(
                "Rotation",0,360,0,-360,0);
        PropertyValuesHolder colorHolder= PropertyValuesHolder.ofInt(
          "BackgroundColor",0xffff0000,0xff00ff00,0xff0000ff, 0xff00ffff
        );
        PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("ScaleX", 1, 2, 0.5F, 1.2F, 0.3F, 1.0F);

        PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("ScaleY", 1, 2, 0.5F, 1.6F, 0.4F, 1.0F);

        PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat(
                "Alpha",1,0,1,0,0.7F,0.2F,1);


        PropertyValuesHolder translationY = PropertyValuesHolder.ofFloat(
          "translationY",0,300,0,-300,0,300
        );
        PropertyValuesHolder translationX = PropertyValuesHolder.ofFloat(
                "translationX",0,100,0,-100,0,100
        );
        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(mText,translationY,rotationHolder,scaleX,scaleY,alpha,colorHolder);


        objectAnimator.setDuration(2000);
        objectAnimator.setInterpolator(new LinearInterpolator());
        objectAnimator.start();
    }

}